package com.example.administrator.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2016/4/8 0008.
 */
public class RadioPhoto extends View{
    private Paint mPaint;
    private Bitmap mBitmap;
    //得到触摸点的坐标
    float x,y;
    public RadioPhoto(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.mm);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //画布的宽高
        int cw = canvas.getWidth();
        int ch = canvas.getHeight();
        //新建一个图层  之后的操作都会绘制在新建的图层上面
        /*
         * 参一参二参三参四：新建的图层的大小
         * 参五：笔
         * 参六:标记
         * 返回值：层的编号
         */

        int sc = canvas.saveLayer(0, 0, cw, ch, mPaint, Canvas.ALL_SAVE_FLAG);
        canvas.drawCircle(x,y,mBitmap.getWidth()/3,mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(mBitmap, 0, 0, mPaint);

        mPaint.setXfermode(null);
        canvas.restoreToCount(sc);
    }

    /*
     * 回调事件，当你触摸此控件，会触发此事件
     * MotionEvent：事件的信息  触摸的位置，触摸的动作等
     */

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                x = event.getX();
                y = event.getY();
                invalidate();
                break;
        }
        return true;
    }
}
